package com.century.sjt.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.bumptech.glide.Glide;
import com.century.sjt.R;
import com.century.sjt.entity.Merchant;
import com.century.sjt.utils.PreferenceManager;
import com.century.sjt.widget.CircleImageView;

import java.util.ArrayList;
import java.util.List;

//import android.widget.ImageView;
//import com.century.sjt.activity.Merchant.MerchantItemActivity;

/**
 * Created by pig33 on 16-01-8.
 */
public class MerchantListAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater;
    private List<Merchant> mDataList;
    private Context context;
    private String aCiry;
    private RequestQueue mQueue;

    //初始化
    public MerchantListAdapter(Context context, List<Merchant> mList, RequestQueue mQueue) {
        this.mDataList = mList;
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.mQueue=mQueue;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int i) {
        return mDataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View convertView, ViewGroup viewGroup) {

        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.sjt_merchant_pay_item, null);
            holder.tvName = (TextView) convertView.findViewById(R.id.item_tv_name);
//            holder.tvDemo = (TextView) convertView.findViewById(R.id.item_tv_mp_list_demo);
            holder.tvCity=(TextView) convertView.findViewById(R.id.item_tv_city);
            holder.nivLogo = (ImageView) convertView.findViewById(R.id.item_img_image);
            holder.icon_position = (TextView) convertView.findViewById(R.id.icon_position) ;
            holder.layoutMp = (LinearLayout) convertView.findViewById(R.id.layout_item_mp);
            holder.tvRebate=(TextView)convertView.findViewById(R.id.item_tv_mp_list_rebate);
            holder.icon_vip = (TextView) convertView.findViewById(R.id.icon_vip);
            holder.tvVip=(TextView)convertView.findViewById(R.id.item_tv_mp_list_vip);
            holder.VIPLayout = (LinearLayout) convertView.findViewById(R.id.item_tv_mp_list_viplayout);
            holder.layout_Level = (LinearLayout) convertView.findViewById(R.id.layout_Level);
            holder.shop_nationalflag= (CircleImageView) convertView.findViewById(R.id.shop_nationalflag);
            holder.shop_logo= (CircleImageView) convertView.findViewById(R.id.shop_logo);

            holder.img1 = (ImageView) convertView.findViewById(R.id.img_1);
            holder.img2 = (ImageView) convertView.findViewById(R.id.img_2);
            holder.img3 = (ImageView) convertView.findViewById(R.id.img_3);
            holder.img4 = (ImageView) convertView.findViewById(R.id.img_4);
            holder.img5 = (ImageView) convertView.findViewById(R.id.img_5);
            Typeface iconfont = Typeface.createFromAsset(context.getAssets(), "iconfont.ttf");
            holder.icon_position.setTypeface(iconfont);
            holder.icon_vip.setTypeface(iconfont);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.tvName.setText(mDataList.get(i).getShopName());
        holder.tvCity.setText(mDataList.get(i).getCity());
        String rebatee = mDataList.get(i).getDiscount();
        String vipDiscount = mDataList.get(i).getVipdiscount();
        String isVIP = mDataList.get(i).getIsVip();
        String vipDiscountDeduct = mDataList.get(i).getVipDiscountDeduct();
        String discountDeduct = mDataList.get(i).getDiscountDeduct();
        String languageCode = PreferenceManager.getInstance().getLanguageCode();
        if ("zh-CN".equals(languageCode)){//中文折扣显示
            if ("10".equals(rebatee)){
                holder.tvRebate.setVisibility(View.GONE);
            }else {
                holder.tvRebate.setText(rebatee+context.getString(R.string.text_discounted));
                holder.tvRebate.setVisibility(View.VISIBLE);
            }
//        if ("1".equals(isVIP)){
//
//        }
            if ("10".equals(vipDiscount)){
                holder.VIPLayout.setVisibility(View.GONE);
                holder.tvVip.setVisibility(View.GONE);
            }else {
                holder.tvVip.setText(vipDiscount+context.getString(R.string.text_discounted));
                holder.VIPLayout.setVisibility(View.VISIBLE);
                holder.tvVip.setVisibility(View.VISIBLE);
            }
        }else {
            if ("0".equals(discountDeduct)){
                holder.tvRebate.setVisibility(View.GONE);
            }else {
                holder.tvRebate.setText(discountDeduct+context.getString(R.string.text_discountDeduct));
                holder.tvRebate.setVisibility(View.VISIBLE);
            }
//        if ("1".equals(isVIP)){
//
//        }
            if ("0".equals(vipDiscountDeduct)){
                holder.VIPLayout.setVisibility(View.GONE);
                holder.tvVip.setVisibility(View.GONE);
            }else {
                holder.tvVip.setText(vipDiscount+context.getString(R.string.text_discountDeduct));
                holder.VIPLayout.setVisibility(View.VISIBLE);
                holder.tvVip.setVisibility(View.VISIBLE);
            }
        }
        holder.imgList.add(holder.img1);
        holder.imgList.add(holder.img2);
        holder.imgList.add(holder.img3);
        holder.imgList.add(holder.img4);
        holder.imgList.add(holder.img5);

        for(int j = 0 ;j<holder.imgList.size() ; j++){
            holder.imgList.get(j).setVisibility(View.GONE);
            //闪烁
            AlphaAnimation alphaAnimation1 = new AlphaAnimation(1.0f, 1.0f);
            alphaAnimation1.setDuration(1000);
            alphaAnimation1.setRepeatCount(Animation.INFINITE);
            alphaAnimation1.setRepeatMode(Animation.REVERSE);
            holder.imgList.get(j).setAnimation(alphaAnimation1);
            alphaAnimation1.start();
        }

        if (mDataList.get(i).getLevel()>5){
            for (int j=0;j<5;j++){
                holder.imgList.get(j).setVisibility(View.VISIBLE);
                holder.imgList.get(j).setImageDrawable(context.getResources().getDrawable(R.mipmap.diamondorange2x));
                //闪烁
                AlphaAnimation alphaAnimation1 = new AlphaAnimation(0.1f, 1.0f);
                alphaAnimation1.setDuration(1000);
                alphaAnimation1.setRepeatCount(Animation.INFINITE);
                alphaAnimation1.setRepeatMode(Animation.REVERSE);
                holder.imgList.get(j).setAnimation(alphaAnimation1);
                alphaAnimation1.start();
            }
        }else {
            for (int j=0;j<mDataList.get(i).getLevel();j++){
                holder.imgList.get(j).setVisibility(View.VISIBLE);
                holder.imgList.get(j).setImageDrawable(context.getResources().getDrawable(R.mipmap.diamondorange2x));
            }
        }


        Glide.with(context).load(mDataList.get(i).getLogo()).into(holder.nivLogo);
        Glide.with(context).load(mDataList.get(i).getNationalFlag()).into(holder.shop_nationalflag);
        Glide.with(context).load(mDataList.get(i).getMerchantLogo()).into(holder.shop_logo);
        holder.layout_Level.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        return convertView;

    }

    class ViewHolder {
        public TextView tvName;
//        public TextView tvDemo;
        public TextView tvCity;
        public ImageView nivLogo;
        public LinearLayout layoutMp;
        public TextView tvRebate;
        public TextView tvVip;
        public LinearLayout VIPLayout;
        private ImageView img1,img2,img3,img4,img5;
        private List<ImageView> imgList = new ArrayList<>();
        private LinearLayout layout_Level;
        private TextView icon_position;
        private TextView icon_vip;
        private CircleImageView shop_logo;
        private CircleImageView shop_nationalflag;
    }
}
